package com.dgshanger.sanhxiaofeisc.mvp.model

import com.dgshanger.sanhxiaofeisc.common.AppApi
import com.dgshanger.sanhxiaofeisc.common.Constants
import com.dgshanger.sanhxiaofeisc.entity.UserEntity
import com.dgshanger.sanhxiaofeisc.mvp.contract.PersonContract
import com.xx.baseutilslibrary.network.entity.BaseResponseEntity
import com.xx.baseutilslibrary.network.rx.RxHelper
import com.xx.baseutilslibrary.network.rx.RxHttpObserver
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

/**
 * PersonModel
 * 沉迷学习不能自拔
 * Describe：
 * Created by 雷小星🍀 on 2018/4/26 17:06.
 */

class PersonModel : PersonContract.Model {

    override fun putUserEntity(entity: UserEntity) {
        Constants.putUserEntity(entity)
    }

    override fun getUserInfo(httpObserver: RxHttpObserver<UserEntity>) {
        AppApi.Api()
                .user_info()
                .compose<BaseResponseEntity<UserEntity>>(RxHelper.io_main<BaseResponseEntity<UserEntity>>())
                .subscribe(httpObserver)
    }

    override fun fileStore(file: File, httpObserver: RxHttpObserver<Any>) {

        val type = RequestBody.create(MultipartBody.FORM, "app_user_avatar")

        val requestBody = RequestBody.create(MultipartBody.FORM, file)
        val newfile = MultipartBody.Part.createFormData("newfile", file.name, requestBody)

        AppApi.Api()
                .file_store(newfile, type)
                .flatMap { filePathEntityBaseResponseEntity ->
                    AppApi.Api()
                            .new_avatar(filePathEntityBaseResponseEntity.data!!.file_path)
                }
                .compose(RxHelper.io_main())
                .subscribe(httpObserver)
    }
}
